package com.example.mybiadushoujiweishi.activity.acivitys.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;


import com.example.mybiadushoujiweishi.R;
import com.example.mybiadushoujiweishi.activity.acivitys.OnekeySerchActivity;
import com.example.mybiadushoujiweishi.activity.acivitys.OnekeyhelpActivity;
import com.example.mybiadushoujiweishi.activity.acivitys.adapter.SOSAdapter;
import com.example.mybiadushoujiweishi.activity.acivitys.entity.Contact;
import com.example.mybiadushoujiweishi.activity.acivitys.utils.MyApplication;


import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/5/26 0026.
 */


public class Onkeyhelphome_fragment extends Fragmentbase {
    @BindView(R.id.add_contacts)
    ImageView addContacts;
    @BindView(R.id.onkey_chebox)
    CheckBox onkeyChebox;
    @BindView(R.id.onkey_bttomlayout)
    RelativeLayout onkeyBttomlayout;
    Unbinder unbinder;

    @BindView(R.id.onekey_relayouttwo)
    RelativeLayout onekeyRelayouttwo;

    @BindView(R.id.edit_onkeyhelp)
    EditText editOnkeyhelp;
    @BindView(R.id.btn_queding)
    Button btnQueding;
    @BindView(R.id.onekey_recyclerview)
    RecyclerView onekeyRecyclerview;

    SOSAdapter sosAdapter;
    @BindView(R.id.btn_suess)
    Button btnSuess;

    @Override
    public int getXml() {

        return R.layout.onekeyhelpfragmenthome;

    }

    @Override
    public void initView() {
        editOnkeyhelp.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                addContacts.setVisibility(View.GONE);
                btnQueding.setVisibility(View.VISIBLE);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {
//                btnQueding.setVisibility(View.INVISIBLE);
//                addContacts.setVisibility(View.VISIBLE);
            }
        });

    }

    @Override
    public void initData() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);


        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.add_contacts)
    public void onViewClicked() {

        getActivity().startActivity(new Intent(MyApplication.getContext(), OnekeySerchActivity.class));


    }

    /**
     *
     */
    @OnClick(R.id.btn_queding)
    public void onViewClick() {
        String phone = editOnkeyhelp.getText().toString().trim();
        if (phone.length() == 11 && phone.matches("^1[3578]\\d{9}$")) {

            List<Contact> listsos = MyApplication.getSoslist();
            Contact contact = new Contact();
            contact.setPhonenum(phone);
            if (listsos.size() < 3) {
                listsos.add(contact);
            } else {
                Toast.makeText(MyApplication.getContext(), "不能超过三个", Toast.LENGTH_SHORT).show();
            }

            sosAdapter = new SOSAdapter(listsos);

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
            onekeyRecyclerview.setLayoutManager(linearLayoutManager);
            onekeyRecyclerview.setAdapter(sosAdapter);

        }


        Toast.makeText(MyApplication.getContext(), "nihao", Toast.LENGTH_SHORT).show();
        editOnkeyhelp.setText("");
        btnQueding.setVisibility(View.GONE);
        addContacts.setVisibility(View.VISIBLE);


       /* Myuitl myuitl = new Myuitl();
        List<Contact> list = myuitl.getAllContacts();*/
    }


    @Override
    public void onResume() {
        super.onResume();
        if (sosAdapter != null) {
            sosAdapter.notifyDataSetChanged();
        }
    }

    @OnClick(R.id.btn_suess)
    public void onViewClickedtwo() {

        if (MyApplication.getSoslist().size()==0){
            Toast.makeText(MyApplication.getContext(), "请选择联系人", Toast.LENGTH_SHORT).show();
        }else
        {
          if (getActivity() instanceof OnekeyhelpActivity){
              OnekeyhelpActivity activity = (OnekeyhelpActivity) getActivity();


              activity.changeFragment(new ThridSosfragment(), "thridfragment");

          }


        }



    }
}
